package coms.dataaccess.hibernate;

import coms.dataaccess.dao.ConferenceItemBookingDAO;
import coms.dto.Conference;
import coms.dto.ConferenceItemBooking;
import coms.dto.ConferenceItemBooking;
import coms.dto.User;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class ConferenceItemBookingDAOImpl extends GenericDAOImpl<ConferenceItemBooking, Integer> implements ConferenceItemBookingDAO {

    private Session session;

    @Override
    public List<ConferenceItemBooking> search(ConferenceItemBooking conferenceItemBooking) throws SQLException {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(ConferenceItemBooking.class, "confItemBooking");
        if (conferenceItemBooking == null) {
            return criteria.list();
        }

        if (conferenceItemBooking.getConferenceItem() != null) {
            criteria.createAlias("confItemBooking.conferenceItem", "conferenceItem").add(Restrictions.eq("conferenceItem.id", conferenceItemBooking.getConferenceItem().getId()));
        }

        if (conferenceItemBooking.getParticipant() != null) {
            criteria.createAlias("confItemBooking.participant", "p").add(Restrictions.eq("p.id", conferenceItemBooking.getParticipant().getId()));
        }
        return criteria.list();
    }

    @Override
    public List<ConferenceItemBooking> searchByParticipantAndConference(User user, Conference conference) {
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(ConferenceItemBooking.class, "confItemBooking");

            System.out.println("User Id :" + user.getId());
            System.out.println("Conference Id:" + conference.getId());
            if (user != null) {
                criteria.createAlias("confItemBooking.participant", "p").add(Restrictions.eq("p.id", user.getId()));
            }

            if (conference != null) {
                criteria.createAlias("confItemBooking.conferenceItem", "confItem").createAlias("confItem.conference", "conf").add(Restrictions.eq("conf.id", conference.getId()));
            }
            System.out.println(criteria.list().size());
            return criteria.list();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return null;
    }
}
